<?php
/**
 * sfJqueryDash base actions.
 *
 * @package    plugins
 * @subpackage sfJqueryDash
 * @author     Ivan Tanev aka Crafty_Shadow @ webworld.bg <vankata.t@gmail.com>
 * @version    SVN: $Id: BasesfJqueryDashActions.class.php 25203 2009-12-10 16:50:26Z Crafty_Shadow $
 */ 
class BasesfJqueryDashActions extends sfActions
{
 
    /**
    * Executes the index action, which shows a list of all available modules
    *
    */
    public function executeDashboard(sfWebRequest $request)
    {
        $this->items = sfJqueryDash::getItems();

        $this->categories = sfJqueryDash::getCategories();

        $this->allowTasks = $this->getUser()->hasCredential('superofficer');
    }

    public function executeServiceImport(sfWebRequest $request)
    {
        $q = Doctrine::getTable("DataImport")
            ->createQuery()->select("COUNT(*)");
        $r = $q->execute();
        $count = $r->getFirst()->toArray();
        $queue = $count["COUNT"];

        $text = "<font style='color: red;'>STOPPED</font>";
        if ($queue > 0)
        {
            $q = Doctrine::getTable("DataImportStatus")
                ->createQuery()->select("COUNT(*)");
            $r = $q->execute();
            $count = $r->getFirst()->toArray();
            $count = $count["COUNT"];

            sleep(5);

            $q = Doctrine::getTable("DataImportStatus")
                ->createQuery()->select("COUNT(*)");
            $r = $q->execute();
            $count2 = $r->getFirst()->toArray();
            $count2 = $count2["COUNT"];

            $text = "<font style='color: green;'>Running</font>";
            if ($count != $count2)
            {
                $text = "<font style='color: green;'>Running</font>";
            }
        }
        return $this->renderText($text);
    }

    public function executeServiceArmory(sfWebRequest $request)
    {
        $q = Doctrine::getTable("DataSync")
            ->createQuery()->select("COUNT(*)");
        $r = $q->execute();
        $count = $r->getFirst()->toArray();
        $queue = $count["COUNT"];

        $text = "<font style='color: red;'>STOPPED</font>";
        if ($queue > 0)
        {
            $q = Doctrine::getTable("DataSyncStatus")
                ->createQuery()->select("COUNT(*)");
            $r = $q->execute();
            $count = $r->getFirst()->toArray();
            $count = $count["COUNT"];

            sleep(15);

            $q = Doctrine::getTable("DataSyncStatus")
                ->createQuery()->select("COUNT(*)");
            $r = $q->execute();
            $count2 = $r->getFirst()->toArray();
            $count2 = $count2["COUNT"];
            if ($count != $count2)
            {
                $text = "<font style='color: green;'>Running</font>";
            }
        }

        return $this->renderText($text);
    }

    public function executeQueueImport(sfWebRequest $request)
    {
        $q = Doctrine::getTable("DataImport")
            ->createQuery()->select("COUNT(*)");
        $r = $q->execute();
        $count = $r->getFirst()->toArray();
        $count = $count["COUNT"];

        $text = $count;
        return $this->renderText($text);
    }

    public function executeQueueArmory(sfWebRequest $request)
    {
        $q = Doctrine::getTable("DataSync")
            ->createQuery()->select("COUNT(*)");
        $r = $q->execute();
        $count = $r->getFirst()->toArray();
        $count = $count["COUNT"];

        $text = $count;
        return $this->renderText($text);
    }
}